package com.elhakimz.matra.scene;

import com.jme.system.canvas.JMECanvas;
import com.jme.system.DisplaySystem;
import com.jme.input.*;
import com.jme.util.GameTaskQueue;
import com.jme.util.GameTaskQueueManager;
import com.jme.renderer.Camera;
import com.jmex.awt.lwjgl.LWJGLAWTCanvasConstructor;
import com.jmex.awt.lwjgl.LWJGLCanvas;
import com.jmex.awt.input.AWTMouseInput;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Logger;
import java.util.concurrent.Callable;

/**
 * Created by IntelliJ IDEA.
 * User: abiel
 * Date: Feb 15, 2010
 * Time: 4:50:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class ScenePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    static final Logger logger = Logger.getLogger(ScenePanel.class.getName());


    private String defaultCursor = "defaultCursor";
    private String attackCursor = "attackCursor";
    private String selectCursor = "selectCursor";
    private String[] cursors = {defaultCursor, attackCursor, selectCursor};
    private int currentCursorIndex;


    MouseLookHandler mouseLook;
    KeyboardLookHandler keyboardLook;

    Camera cam;
    Mouse mouse;
    int camSpeed = 100;


    InputHandler input;
    static Canvas comp = null;
    SceneImpl impl;
    DisplaySystem display;

    public ScenePanel(Dimension dimension) {
        setLayout(new BorderLayout());
        String renderer = "LWJGL";
        display = DisplaySystem.getDisplaySystem();
        
        display.registerCanvasConstructor("AWT", LWJGLAWTCanvasConstructor.class);
        comp = (Canvas) display.createCanvas(
                dimension.width, dimension.height);
        display.setVSyncEnabled(true);
        // add a listener... if window is resized, we can do something about it.
        comp.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent ce) {
                doResize();
            }
        });

        // MAKE SURE YOU REPAINT SOMEHOW OR YOU WON'T SEE THE UPDATES...
        new Thread() {

            {
                setDaemon(true);
            }

            @Override
            public void run() {

                try {
                    while (true) {
                        comp.repaint();
                        Thread.sleep(20);
                        yield();
                    }
                } catch (Exception e) {

                    logger.throwing(this.getClass().toString(), "run()", e);

                }
            }
        }.start();

        setupInput();

        impl = new SceneImpl(dimension.width, dimension.height);
        LWJGLCanvas jmeCanvas = ((LWJGLCanvas) comp);
//        jmeCanvas.setTargetRate(60);
        jmeCanvas.setImplementor(impl);
        jmeCanvas.setUpdateInput(true);
        //jmeCanvas.setBounds(0, 0, dimension.width, dimension.height);


        Callable<?> exe = new Callable() {
            public Object call() {
                forceUpdateToSize();
                return null;
            }
        };
        GameTaskQueueManager.getManager().getQueue(GameTaskQueue.RENDER).enqueue(exe);
        add(jmeCanvas);
    }


    protected void doResize() {


        if (impl != null) {
            impl.resizeCanvas(comp.getWidth(), comp.getHeight());
            ((JMECanvas) comp).makeDirty();
            if (impl.getCamera() != null) {
                Callable<?> exe = new Callable() {
                    public Object call() {
                         return null;
                    }
                };
                GameTaskQueueManager.getManager().getQueue(GameTaskQueue.RENDER).enqueue(exe);
            }
        }

        //impl.resizeCanvas(comp.getWidth(), comp.getHeight());
    }

    public void forceUpdateToSize() {
        comp.setSize(comp.getWidth(), comp.getHeight() + 1);
        comp.setSize(comp.getWidth(), comp.getHeight() - 1);

    }


    public void setupInput() {
        // Aktiviere Eingabemoeglichkeiten fuer Maus und Tastatur!
        if (!KeyInput.isInited()) {
            KeyInput.setProvider(InputSystem.INPUT_SYSTEM_AWT);
            KeyListener kl = (KeyListener) KeyInput.get();
            comp.addKeyListener(kl);
        }


        if (!MouseInput.isInited())
            MouseInput.setProvider(InputSystem.INPUT_SYSTEM_AWT);

        ((AWTMouseInput) MouseInput.get()).setDragOnly(true);
        comp.addMouseListener((MouseListener) MouseInput.get());
        comp.addMouseMotionListener((MouseMotionListener) MouseInput.get());


    }

    public static Canvas getComp() {
        return comp;
    }
}